package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.bean.auth.CommonUserAuthorization;
import cn.edu.seu.herald.informationplatform.bean.auth.LeagueSession;
import cn.edu.seu.herald.informationplatform.bean.auth.LeagueUserAuthorization;
import cn.edu.seu.herald.informationplatform.bean.auth.UserSession;
import cn.edu.seu.herald.informationplatform.dao.database.LeagueInfoDao;
import cn.edu.seu.herald.informationplatform.dao.database.LeagueSessionDao;
import cn.edu.seu.herald.informationplatform.dao.database.LeagueUserDao;
import cn.edu.seu.herald.informationplatform.dao.database.UserDao;
import cn.edu.seu.herald.informationplatform.dao.database.UserSessionDao;
import cn.edu.seu.herald.informationplatform.filter.UserAuthorized;

public class LogOut extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		try {
			deleteSession(request, response);
		} catch (NumberFormatException e) {
		} catch (SQLException e) {
		}
		
		Cookie cookie1 = new Cookie("LEAGUE_USER_SESSION_ID", null);
		cookie1.setMaxAge(0);
		cookie1.setPath("/");
		response.addCookie(cookie1);
		Cookie cookie2 = new Cookie("COMMON_USER_SESSION_ID", null);
		cookie2.setMaxAge(0);
		cookie2.setPath("/");
		response.addCookie(cookie2);

		String url = request.getParameter("url");
		System.out.println("URL:"+url);
		if (url == null) {
			response.sendRedirect("/");
		} else {
			response.sendRedirect(url);
		}
	}
	
	private void deleteSession(HttpServletRequest request,
			HttpServletResponse response) throws NumberFormatException, SQLException {
		Cookie[] cookies = request.getCookies();
		boolean userFlag = true;
		boolean leagueFlag = true;
		if (cookies != null) {
			for (Cookie cookie: cookies) {
				String name = cookie.getName();
				if (userFlag &&
						name.equalsIgnoreCase(UserAuthorized.COMMON_USER_SESSION_ID_COOKIE_NAME)) {
					String value = cookie.getValue();
					UserSessionDao dao = UserSessionDao.getDao();
					if (dao.contains(Integer.valueOf(value))) {
						dao.deleteUserSession(Integer.valueOf(value));
					}
					userFlag = false;
				}
				if (leagueFlag &&
						name.equalsIgnoreCase(UserAuthorized.LEAGUE_USER_SESSION_ID_COOKIE_NAME)) {
					String value = cookie.getValue();
					LeagueSessionDao dao = LeagueSessionDao.getDao();
					if (dao.contains(Integer.valueOf(value))) {
						dao.deleteLeagueSession(Integer.valueOf(value));
					}
					leagueFlag = false;
				}
			}
		}
	}

}
